﻿using GalaSoft.MvvmLight.Messaging;
using Mina.Core.Session;
using TimerJobServerManager.Editer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TimerJobServer.Common.Mina;

namespace TimerJobServerManager
{
    /// <summary>
    /// ConfigWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigWindow
    {
        public ConfigWindow()
        {
            InitializeComponent();
            IsRemote = false;
        }

        /// <summary>
        /// 表示是否是远程文件
        /// </summary>
        public bool IsRemote
        {
            get
            { return editerControl.IsRemote; }
            set
            {
                editerControl.IsRemote = value;
            }
        }

        private void GetRemoteFiles(ObservableCollection<DirectoryRecord> obj)
        {
            directoryTreeView.ItemsSource = obj; 
        }

        public bool OpenFile(string fileName)
        {
            return editerControl.OpenFile(fileName);
        }


        /// <summary>
        /// 添加文件和目录
        /// </summary>
        private void AddFile(DirectoryInfo directoryInfo, DirectoryRecord record)
        {
            if (directoryInfo.Exists)
            {
                foreach (var dir in directoryInfo.GetDirectories())
                {
                    var dircon = new DirectoryRecord()
                    {
                        FileName = dir.Name,
                        FileFullName = dir.FullName
                    };
                    record.Nodes.Add(dircon);
                    AddFile(dir, dircon);
                }

                foreach (var file in directoryInfo.GetFiles())
                {
                    record.Nodes.Add(new DirectoryRecord()
                    {
                        FileName = file.Name,
                        FileFullName = file.FullName
                    });
                }
            }
        }

        public void OpenDirectory(string dirPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            var directory = new ObservableCollection<DirectoryRecord>();
            var directoryRoot = new DirectoryRecord();
            directoryRoot.FileName = directoryInfo.Name;
            directoryRoot.FileFullName = directoryInfo.FullName;
            directoryRoot.IsExpanded = true;
            AddFile(directoryInfo, directoryRoot);
            directory.Add(directoryRoot);
            directoryTreeView.ItemsSource = directory;
        }
        /// <summary>
        /// 设置树目录
        /// </summary>
        /// <param name="source"></param>
        public void SetDirTreeView(System.Collections.IEnumerable source)
        {
            //this.lbl.Dispatcher.Invoke(new Action(() => { this.lbl.Text = "this is a test!!!"; }));
            directoryTreeView.Dispatcher.Invoke(new Action(() =>
            {
                directoryTreeView.ItemsSource = source;
            }));
        }
        /// <summary>
        /// 设置内容
        /// </summary>
        /// <param name="content"></param>
        public void SetContent(string content)
        {
            directoryTreeView.Dispatcher.Invoke(new Action(() =>
            {
                editerControl.textEditor.Text = content;
            }));
        }

            private void directoryTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (directoryTreeView.SelectedItem != null)
            {
                DirectoryRecord record = (DirectoryRecord)directoryTreeView.SelectedItem;
                string path = record.FileFullName;
                string ext = System.IO.Path.GetExtension(path);//判断后缀是否可以读取
                string[] canOpen = { "txt", "log", "config", "xml", "md", "cs", "log", "json","bak","java" };
                bool isOk = false;
                foreach (var item in canOpen)
                {
                    if (ext.Contains(item))
                    {
                        isOk = true;
                        break;
                    }
                }
                if (isOk==false)
                {
                    return;
                }

                if (IsRemote)
                {
                    editerControl.OpenRemoteFile(path);
                }
                else
                {
                    OpenFile(path);
                }
            }
        }
        public IoSession Session
        {
            get { return editerControl.Session; }
            set { editerControl.Session = value; }
        }

      
    }
}
